package org.zsl.mqdemo.pattern.obsever2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WexinSubjcet implements Subject {

    private Map<String,List<Observer>> observerList;

    public WexinSubjcet(){

        observerList = new HashMap<>();
    }


    @Override
    public void attach(Observer observer, String topic) {

        List<Observer> list = observerList.get(topic);
        if(list==null){

            list = new ArrayList<>();
            observerList.put(topic,list);
        }

        list.add(observer);
    }

    @Override
    public void detach(Observer observer, String topic) {


        List<Observer> list = observerList.get(topic);

        if(list!=null){
            list.remove(observer);
        }


    }

    @Override
    public void nodify(String message,String topic) {


        List<Observer> list = observerList.get(topic);

        if(list!=null){

            for(Observer observer:list){

                 observer.update(message + "from " + topic);
            }
        }


    }
}
